package com.CGFrame.struts.bean.keypath;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.jfree.data.gantt.Task;

import com.CGFrame.struts.POJOs.Make;
import com.CGFrame.struts.bean.make.MakeBean;
import com.CGFrame.struts.bean.unit.UnitBean;

public class DataFactory {
	private int objectId;
	private String type;
	private Date startTime;
	private UnitBean unitBean ;
	
	public DataFactory(int objectId,String type,Date startTime)
	{
		this.objectId = objectId;
		this.type = type;
		this.startTime = startTime;
		this.unitBean = new UnitBean(type,objectId);
		startTime.setHours(8); // 设置为8点上班
	}
	public List<TaskNode> getScheduleDatas()
	{
		List<TaskNode> list = new LinkedList<TaskNode>();
		KeypathBean keypathBean = new KeypathBean();
		
		EventNode[] makes = keypathBean.Init(unitBean.getObject(), type);
		int length = makes.length;
	//	TaskNode [] tasks  = new TaskNode[length];
		for(int i=0;i<length;i++)
		{
			int makeId = makes[i].getId();
			String makeName = makes[i].getName();
			int makeVe = makes[i].getVe();
			int rest = makes[i].getRest();
			Date makeSTime = this.dateAddHour(startTime,
					makeVe);
			Date makeETime = this.dateAddHour(startTime,
					makeVe + rest);
			String tip = makes[i].getTip();
			double percent = makes[i].getPercent();
			TaskNode taskNode = new TaskNode(makeName,makeSTime,makeETime,tip,percent);
			
			list.add(taskNode);
		}
		Collections.sort(list);
		return list;
	}
	
	public List<TaskNode> getActualDatas()
	{
		List<TaskNode> list = new LinkedList<TaskNode>();
		Iterator ite_Makes = unitBean.getExistMakes().iterator();
		while (ite_Makes.hasNext())
		{
			MakeBean makeBean  = new MakeBean( (Make)ite_Makes.next() );			
			TaskNode taskNode = new TaskNode(makeBean.getName(),makeBean.getExactStartTime(),makeBean.getExactEndTime(),makeBean.getTips(),makeBean.getPercent());
			list.add(taskNode);
		}
		Collections.sort(list);
		return list;
	}
	private Date dateAddHour(Date date , int totalHour)//�����ĩ
	{
		int day = totalHour/8;
		int hour = totalHour%8;
		GregorianCalendar  cal = new GregorianCalendar();
		cal.setTime(date);
		int dayOfWeek1 = cal.get(Calendar.DAY_OF_WEEK); //�ܼ�
//		if (dayOfWeek1 == 1 )cal.add(GregorianCalendar.DATE, 1);
//		if (dayOfWeek1 == 7 )cal.add(GregorianCalendar.DATE, 2);
		for (int i = 1 ; i<=day ; i++)
		{
			cal.add(GregorianCalendar.DATE, 1);
			int dayOfWeek2 = cal.get(Calendar.DAY_OF_WEEK); //��1����ܼ�

			if (dayOfWeek2 == 7 )cal.add(GregorianCalendar.DATE, 2); //�������ټ�2��
		}
		cal.add(GregorianCalendar.HOUR, hour);
		int dayOfWeek3 = cal.get(Calendar.DAY_OF_WEEK); //��hour���ܼ�
//		if (dayOfWeek3 == 7 )cal.add(GregorianCalendar.DATE, 2); //�������ټ�2��
		return cal.getTime();
	}
}
